﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI;
using Microsoft.Practices.Unity;
using Baosight.ZNH.ZP.UI.Utils;

namespace Baosight.ZNH.ZP.UI
{

    public partial class ZP08 : EFViewCmes
    {
        private List<EFDataField> dataFields;//区域的对象list
        private BaseBlock baseBlock = new BaseBlock();//构造前台是否下拉列表
        //获得输入区域的所有对象list
        private List<EFDataField> DataFields
        {
            get
            {
                if (dataFields == null)
                {
                    dataFields = this.ORG.GetChildObjects<EFDataField>(null, true);
                }
                return dataFields;
            }
        }

        //临时存储按钮对象
        private Button TmpBut
        {
            get;
            set;
        }
        public ZP08()
        {
            InitializeComponent();
        }

        public override void Initialize(EIInfo initData)
        {
            initData.AddBlock(baseBlock.GetScDepartmentBlock());
            base.Initialize(initData);
        }
        
        /// <summary>
        /// 点击“修改”按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            Button but = sender as Button;
            but.IsEnabled = false;
            if (!this.ORG.ValidateField())
            {
                this.ValidateMessagePopup();
                but.IsEnabled = true;
                return;
            }
            else
            {
                //构造EIInfo
                EIInfo subInfo = new EIInfo();
                subInfo.AddBlock(EIConstants.ResultBlock).SetColumns(GetColumns());
                subInfo.Attributes.Add(EIConstants.ServiceNameKey, "ZP08");
                subInfo.Attributes.Add(EIConstants.MethodNameKey, "update");
                subInfo.GetBlock(EIConstants.ResultBlock).AddRow(GetRowValues());
                this.Submit(subInfo, SubmitCompleted);
                TmpBut = but;
            }
        }

        /// <summary>
        /// 确定按钮后的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SubmitCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                this.OnCommitMessagePopup(e.EIInfo);
            }
            if (TmpBut != null)
            {
                TmpBut.IsEnabled = true;
            }
            e.EIInfo.Set("mode", "update");
            this.UpdateHomePageData(e.EIInfo);//发布数据
        }

        /// <summary>
        /// 重写基类方法：根据数据的改变更新主页面的数据
        /// </summary>
        /// <param name="data"></param>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
            refresh(true);
        }

        /// <summary>
        ///获取DataFieldColumns，不带参数的
        /// </summary>
        public Dictionary<string, EIColumn> GetColumns()
        {
            Dictionary<string, EIColumn> columns = new Dictionary<string, EIColumn>();
            foreach (var _field in DataFields)
            {
                EIColumn onecolumn = new EIColumn();
                onecolumn.Name = _field.eBindingName.Substring(9);
                onecolumn.Desc = _field.eLabel as string;
                columns.Add(_field.eBindingName, onecolumn);
            }
            return columns;
        }

        /// <summary>
        ///获取行的值
        /// </summary>
        public EIRow GetRowValues()
        {
            EIRow row = new EIRow();
            foreach (var _field in DataFields)
            {
                row.Add(_field.eBindingName, _field.Value);
            }
            return row;
        }

        /// <summary>
        /// 页签切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EFTab_SelectionChanged(object sender, RoutedEventArgs e)
        {
            refresh(false);
        }

        private void refresh(bool isUpate)
        {
            Dictionary<string, string> _args = new Dictionary<string, string>();
            _args.Add("orgCode", this.EIDataContext.GetBlock("result").GetRow(0)["name"].ToString());
            _args.Add("orgName", this.EIDataContext.GetBlock("result").GetRow(0)["displayName"].ToString());
            if (isUpate)
            {
                _args.Add("isUpate", "是");
            }
            else
            {
                _args.Add("isUpate", "否");
            }
            this.personInfor.LoadView<EFTabItem>("ZP09", _args);
        }
    }
}
